/*
 * Copyright © 2022
 * @Author: zhangxiang
 * @Date: 2022-07-19 09:42:10
 * @LastEditors: zhangxiang
 * @LastEditTime: 2022-11-03 15:11:46
 * @Description:
 *
 */

/**
 *
 * @description: param2Obj
 * @param {*} url
 * @return {*}
 *
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach((v) => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

/**
 *
 * @description: 判断字符是否为空的方法
 * @param {*} obj
 * @return {*}
 *
 */
export function isEmptyString(obj) {
  if (typeof obj === 'undefined' || obj == null || obj === '') {
    return true
  } else {
    return false
  }
}

/**
 *
 * @description: 递归获取路由路径
 * @param {*} router
 * @param {*} pp
 * @return {*}
 *
 */
export function getRouterPath(router, pp) {
  var arr = []
  pp = pp || ''
  for (const r of router) {
    let path = r.path
    const children = r.children
    if (pp) {
      path = `${pp}/${path}`
    }
    // 如果有子元素，不添加父元素的路径
    if (children && children.length > 0) {
      arr = arr.concat(getRouterPath(children, path)) // 递归获取
    } else {
      arr.push(path)
    }
  }
  return arr
}

/**
 *
 * @description: 格式化size
 * @param {*} size
 * @return {*}
 *
 */
export function formatSize(size) {
  if (size < 1024) {
    return size.toFixed(0) + ' bytes'
  } else if (size < 1024 * 1024) {
    return (size / 1024.0).toFixed(0) + ' KB'
  } else if (size < 1024 * 1024 * 1024) {
    return (size / 1024.0 / 1024.0).toFixed(1) + ' MB'
  } else {
    return (size / 1024.0 / 1024.0 / 1024.0).toFixed(1) + ' GB'
  }
}
